package com.web;

import com.Dao.UserMapper;
import com.pojo.User;
import com.utils.sqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class loginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

     /*   获得页面提供的信息*/
        String userCode = request.getParameter("userCode");
        String pwd = request.getParameter("userPassword");

/*
        实现一个静态的工具类来实现获取SqlSession
*/
        SqlSession sqlSession = sqlSessionUtil.creatSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        User user= new User();
        user.setUserCode(userCode);
        user.setUserPassword(pwd);

        User user1 = userMapper.loginCheck(user);
        if (user1!=null){
            request.getRequestDispatcher("UserServlet?action=sel&userName=admin").forward(request,response);
           /* 跳转到另一个页面*/
           // response.sendRedirect();
        }else{
            /*回到本页面*/
            request.getSession().setAttribute("error","密码账户不匹配");
           /* 发送到 login.jsp*/
            response.sendRedirect("login.jsp");
        }


    }
}
